<template>
    <div class="chat-app">
        <div class="app-header">
            <div class="app-title">
                <span class="logo-char">好友私聊</span>
                <span v-if="username" class="current-chat">· {{ username }}</span>
            </div>
        </div>

        <div class="app-main">
            <div class="sidebar" :class="{'sidebar-collapsed': isSidebarCollapsed}">
                <div class="sidebar-toggle" @click="toggleSidebar">
                    <i :class="isSidebarCollapsed ? 'el-icon-arrow-right' : 'el-icon-arrow-left'"></i>
                </div>

                <div class="sidebar-content" v-show="!isSidebarCollapsed">
                    <div class="user-search">
                        <el-input
                            placeholder="搜索联系人"
                            prefix-icon="el-icon-search"
                            v-model="searchQuery"
                            clearable
                        ></el-input>
                    </div>

                    <div class="contact-list">
                        <div
                            v-for="user in filteredUsers"
                            :key="user.userEmail"
                            class="contact-item"
                            :class="{'contact-active': active && active.userEmail === user.userEmail}"
                            @click="checkChat(user)"
                        >
                            <div class="avatar-container">
                                <el-avatar :size="40" :src="user.userPicture"></el-avatar>
                                <div class="status-indicator" :class="{'online': true}"></div>
                            </div>
                            <div class="contact-info">
                                <div class="contact-name">
                                    {{ user.userName }}
                                    <span class="user-rank" :class="getRankClass(user.userRank)">
                                        {{ getRankLabel(user.userRank) }}
                                    </span>
                                    <span  v-if="$store.getters.unreadCount(user.userEmail) > 0" class="unread-badge">
                                        {{ $store.getters.unreadCount(user.userEmail) }}
                                    </span>
                                </div>
                                <div class="last-message">点击开始聊天</div>
                            </div>
                        </div>

                        <div class="empty-state" v-if="filteredUsers.length === 0 && !loading">
                            <i class="el-icon-user-solid"></i>
                            <p>{{ searchQuery ? '未找到匹配的联系人' : '暂无联系人' }}</p>
                            <el-button type="primary" plain icon="el-icon-plus" size="small">添加好友</el-button>
                        </div>
                    </div>
                </div>
            </div>

            <div class="chat-area">
                <router-view />

                <div class="welcome-view" v-if="!active">
                    <div class="welcome-content">
                        <div class="welcome-icon">
                            <i class="el-icon-chat-dot-round"></i>
                        </div>
                        <h2>开始您的私聊</h2>
                        <p>从左侧选择一位好友开始聊天</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import {getUserlist} from "@/api/api";
import { getRankClass, getRankLabel } from "@/utils/rankUtils";

export default {
    data() {
        return {
            a:"好",
            b:"友",
            c:"私",
            d:"聊",
            username:"",
            userList: [],
            active: null,
            loading: false,
            searchQuery: "",
            isSidebarCollapsed: false
        };
    },
    computed: {
        filteredUsers() {
            if (!this.searchQuery) return this.userList;

            const query = this.searchQuery.toLowerCase();
            return this.userList.filter(user =>
                user.userName.toLowerCase().includes(query) ||
                user.userEmail.toLowerCase().includes(query)
            );
        }
    },
    methods: {
        getRankClass,
        getRankLabel,
        toggleSidebar() {
            this.isSidebarCollapsed = !this.isSidebarCollapsed;
        },
        checkChat(user) {
            this.active = user;
            this.username = user.userName;
            // 标记该用户的消息为已读
            this.$store.commit('markAsRead', user.userEmail);
            this.$router.push({ path: '/Main/privateChat/chat', query: { count: user} });
        },
        async initUserList() {
            try {
                this.loading = true;
                const response = await getUserlist();

                if (response && response.data && response.data.message === "成功") {
                    this.userList = response.data.data;
                    this.$message.success("获取好友列表成功");
                } else {
                    this.$message.error("获取好友列表失败");
                }
            } catch (error) {
                console.error('Error fetching user list:', error);
                this.$message.error("获取好友列表失败，请稍后再试");
            } finally {
                this.loading = false;
            }
        }
    },
    created() {
        this.initUserList();

        // 监听WebSocket消息，处理未读消息计数
        if (this.$store.state.websocket) {
            const originalOnMessage = this.$store.state.websocket.onmessage;
            this.$store.state.websocket.onmessage = (event) => {
                if (originalOnMessage) originalOnMessage(event);

                const message = JSON.parse(event.data);
                    // 如果不是当前正在查看的聊天，则增加未读消息计数
                // if (!this.active || message.from !== this.active.userEmail) {
                //      this.$store.commit('incrementUnread', message.from);
                //  }
            };
        }
    },
};
</script>

<style>
:root {
    --primary-color: #1a73e8;
    --primary-light: #e8f0fe;
    --primary-gradient: linear-gradient(135deg, #1a73e8, #6c92f4);
    --text-primary: #202124;
    --text-secondary: #5f6368;
    --background-light: #f8f9fa;
    --border-color: #dadce0;
    --success-color: #34a853;
    --danger-color: #ea4335;
    --sidebar-width: 280px;
    --sidebar-collapsed-width: 60px;
    --header-height: 60px;
    --shadow-sm: 0 1px 2px 0 rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);
    --shadow-md: 0 2px 6px 2px rgba(60, 64, 67, 0.15);
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

.chat-app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    background-color: var(--background-light);
    color: var(--text-primary);
    overflow: hidden;
}

.app-header {
    height: var(--header-height);
    background-color: white;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    padding: 0 20px;
    box-shadow: var(--shadow-sm);
    z-index: 10;
}

.app-title {
    font-size: 20px;
    font-weight: 500;
    display: flex;
    align-items: center;
}

.logo-char {
    color: var(--primary-color);
    border-bottom: 2px solid var(--primary-color);
    padding-bottom: 2px;
}

.current-chat {
    font-weight: 400;
    color: var(--text-secondary);
    margin-left: 8px;
}

.app-main {
    flex: 1;
    display: flex;
    overflow: hidden;
}

.sidebar {
    width: var(--sidebar-width);
    background-color: white;
    border-right: 1px solid var(--border-color);
    transition: width 0.3s ease;
    position: relative;
    display: flex;
}

.sidebar-collapsed {
    width: var(--sidebar-collapsed-width);
}

.sidebar-toggle {
    position: absolute;
    top: 12px;
    right: -12px;
    width: 24px;
    height: 24px;
    background-color: white;
    border: 1px solid var(--border-color);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 5;
    box-shadow: var(--shadow-sm);
}

.sidebar-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.user-search {
    padding: 16px;
    border-bottom: 1px solid var(--border-color);
}

.user-search .el-input__inner {
    border-radius: 20px;
    background-color: var(--background-light);
    border: none;
    padding-left: 40px;
}

.user-search .el-input__prefix {
    left: 12px;
}

.contact-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.contact-item {
    position: relative;
    display: flex;
    align-items: center;
    padding: 12px;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: background-color 0.2s;
    margin-bottom: 4px;
}

.contact-item:hover {
    background-color: var(--background-light);
}

.contact-active {
    background-color: var(--primary-light) !important;
}

.avatar-container {
    position: relative;
    margin-right: 12px;
}

.status-indicator {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    border: 2px solid white;
    background-color: #ccc;
}

.status-indicator.online {
    background-color: var(--success-color);
}

.contact-info {
    flex: 1;
    min-width: 0;
}

.contact-name {
    font-weight: 500;
    color: var(--text-primary);
    margin-bottom: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: flex;
    align-items: center;
}

/* 用户等级样式 */
.user-rank {
    display: inline-block;
    font-size: 11px;
    font-weight: bold;
    padding: 1px 5px;
    border-radius: 3px;
    margin-left: 4px;
    color: white;
    text-shadow: 0 1px 1px rgba(0, 0, 0, 0.3);
    vertical-align: middle;
}

.rank-d {
    background: linear-gradient(135deg, #4caf50, #2e7d32);
}

.rank-c {
    background: linear-gradient(135deg, #2196f3, #0d47a1);
}

.rank-b {
    background: linear-gradient(135deg, #9c27b0, #4a148c);
}

.rank-a {
    background: linear-gradient(135deg, #424242, #212121);
}

.rank-a-plus {
    background: linear-gradient(135deg, #ff9800, #e65100);
}

.rank-s {
    background: linear-gradient(135deg, #ffd700, #ff6f00);
    color: #5d4037;
}

/* Ant Design样式覆盖 */
.last-message {
    font-size: 13px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 180px;
}

.unread-badge {
    position: absolute;
    top: 8px;
    right: 12px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background-color: var(--danger-color);
    color: white;
    font-size: 12px;
    font-weight: 500;
    border-radius: 12px;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    transition: all 0.3s ease;
    box-shadow: 0 2px 4px rgba(234, 67, 53, 0.2);
}

.contact-item {
    position: relative;
    display: flex;
    align-items: center;
    padding: 12px;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: background-color 0.2s;
    margin-bottom: 4px;
}

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px 20px;
    text-align: center;
    color: var(--text-secondary);
}

.empty-state i {
    font-size: 48px;
    margin-bottom: 16px;
    color: var(--border-color);
}

.empty-state p {
    margin-bottom: 16px;
}

.chat-area {
    flex: 1;
    position: relative;
    overflow: hidden;
    background-color: var(--background-light);
}

.welcome-view {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}

.welcome-content {
    text-align: center;
}

.welcome-icon {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background: var(--primary-gradient);
    color: white;
    font-size: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 24px;
}

.welcome-view h2 {
    font-size: 24px;
    font-weight: 500;
    margin-bottom: 8px;
    color: var(--text-primary);
}

.welcome-view p {
    font-size: 16px;
    color: var(--text-secondary);
}

@media (max-width: 768px) {
    .sidebar:not(.sidebar-collapsed) {
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        z-index: 100;
        box-shadow: var(--shadow-md);
    }

    .sidebar-collapsed {
        width: 0;
    }

    .sidebar-toggle {
        right: -12px;
    }
}

/* Ant Design样式覆盖 */
.ant-tooltip {
    max-width: 250px;
}

.ant-tooltip-inner {
    min-height: 24px;
    padding: 6px 8px;
    color: #fff;
    text-align: center;
    text-decoration: none;
    word-wrap: break-word;
    background-color: rgba(0, 0, 0, 0.75);
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

@media (max-width: 768px) {
    .header-title {
        font-size: 18px;
    }

    .username {
        font-size: 14px;
        margin-left: 5px;
    }
}
</style>
